package com.sora.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sora.commonutils.JwtUtils;
import com.sora.commonutils.exceptionhandler.GuliException;
import com.sora.educenter.entity.UcenterMember;
import com.sora.educenter.entity.vo.LoginVo;
import com.sora.educenter.entity.vo.RegisterVo;
import com.sora.educenter.mapper.UcenterMemberMapper;
import com.sora.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sora.commonutils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author sora
 * @since 2023-01-12
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public String login(LoginVo loginVo) {
        String mail = loginVo.getMail();
        String password = loginVo.getPassword();
        if (StringUtils.isEmpty(mail) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001,"邮箱或密码为空");
        }
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mail",mail);
        UcenterMember one = baseMapper.selectOne(wrapper);
        if (one == null) {
            throw new GuliException(20001,"邮箱不存在");
        }
        if (!MD5.encrypt(password).equals(one.getPassword())) {
            throw new GuliException(20001,"密码不正确");
        }
        if (one.getIsDisabled()) {
            throw new GuliException(20001,"用户被禁用");
        }
        String token = JwtUtils.getJwtToken(one.getId(), one.getNickname());
        return token;
    }

    @Override
    public void register(RegisterVo registerVo) {
        String mail = registerVo.getMail();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
        System.out.println("手动输入的验证码"+code);
        if (StringUtils.isEmpty(mail)
                || StringUtils.isEmpty(nickname)
                || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(code)) {
            throw new GuliException(20001,"用户信息不完整");
        }
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mail",mail);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            throw new GuliException(20001,"邮箱已注册");
        }
        String s = redisTemplate.opsForValue().get("verify:code:" + mail);
        System.out.println("redis里面的验证码" + s);
        if (!code.equals(s)) {
            throw new GuliException(20001,"验证码不正确");
        }
        UcenterMember member = new UcenterMember();
        //密码加密
        member.setPassword(MD5.encrypt(password));
        member.setMail(mail);
        member.setNickname(nickname);
        //用户不禁用
        member.setIsDisabled(false);
        member.setAvatar("https://guli-edu-sora.oss-cn-chengdu.aliyuncs.com/2023/01/01/6968340fbe0346a785385d1360b4cbffAnna%20Lisa.jpg");
        baseMapper.insert(member);
    }

    @Override
    public Integer getCountRegister(String day) {
        return baseMapper.getCountRegister(day);
    }
}
